package com.singfan.common.nosql.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Author: hansonghe
 * Date: 2015-08-21 10:04
 * Desc:
 */
public abstract class MongoDaoBase {
    /**
     * 获取数据库引用
     * @return 数据库引用
     */
    protected abstract String getMongoDatabaseName();

    /**
     * 获取数据库引用
     * @return 数据库引用
     */
    protected MongoDatabase getMongoDatabase() {
        return MongoContext.getMongoClientProvider().getMongoClient().getDatabase(getMongoDatabaseName());
    }

    /**
     * 插入一个DBObject数据
     * @param collectionName 集合名称
     * @param document Document数据
     * @return true:插入成功    false:插入失败
     */
    public boolean insertOne(String collectionName, Document document) {
        MongoDatabase mongoDatabase = getMongoDatabase();

        MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
        collection.insertOne(document);

        return true;
    }

    public boolean insertObj(String collectionName, DBObject document) {
        DB db = MongoContext.getMongoClientProvider().getMongoClient().getDB(getMongoDatabaseName());
        DBCollection collection = db.getCollection(collectionName);
        collection.insert(document);

        return true;
    }

    public List<DBObject> selectList(String collectionName, BasicDBObject basicDBObject, int pageIndex, int pageSize) {
        DB db = MongoContext.getMongoClientProvider().getMongoClient().getDB(getMongoDatabaseName());
        DBCollection collection = db.getCollection(collectionName);
        Iterator<DBObject> dbObjectIterator = collection.find(basicDBObject).skip(pageIndex * pageSize).limit(pageSize).iterator();
        List<DBObject> dbObjectList = new ArrayList<>();
        while (dbObjectIterator.hasNext()){
            dbObjectList.add(dbObjectIterator.next());
        }

        return dbObjectList;
    }

    /**
     *
     * @param collectionName
     * @param document
     * @return
     */
    public List<Document> selectList(String collectionName, Document document) {
        MongoDatabase mongoDatabase = getMongoDatabase();

        MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
        MongoCursor<Document> cursor = collection.find(document).iterator();
        List<Document> documentList = new ArrayList<>();
        while (cursor.hasNext()){
            documentList.add(cursor.next());
        }
        cursor.close();

        return documentList;
    }

    public List<Document> selectList(String collectionName, Document document, int pageIndex, int pageSize) {
        MongoDatabase mongoDatabase = getMongoDatabase();

        MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
        MongoCursor<Document> cursor = collection.find(document).skip(pageIndex * pageSize).limit(pageSize).iterator();
        List<Document> documentList = new ArrayList<>();
        while (cursor.hasNext()){
            documentList.add(cursor.next());
        }
        cursor.close();

        return documentList;
    }
}
